﻿using UnityModule;
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;



//简易Web请求
public class WebRequestHelper
{
    private Action<bool, string> m_callBack;//成功/失败, 内容
    private Coroutine m_coroutine;

    //GET
    public void Get(string url, string data, Action<bool, string> callBack)
    {
        if (string.IsNullOrEmpty(url))
            return;
        m_callBack = callBack;
        if(!string.IsNullOrEmpty(data))
            url += "?" + data;
        m_coroutine = CoroutineComponent.Instance.StartCoroutine(DoGetRequest(url));
    }
    public void Get(string url, string data, Action<bool, string> callBack,int time)
    {
        if (string.IsNullOrEmpty(url))
            return;
        m_callBack = callBack;
        if (!string.IsNullOrEmpty(data))
            url += "?" + data;
        m_coroutine = CoroutineComponent.Instance.StartCoroutine(DoGetRequest(url, time));
    }
    //POST
    public void Post(string url, string data, Action<bool, string> callBack)
    {
        if (string.IsNullOrEmpty(url))
            return;
        m_callBack = callBack;
        m_coroutine = CoroutineComponent.Instance.StartCoroutine(DoPostRequest(url, data));
    }

    public void Post(string url, string data, Action<bool, string> callBack, int time)
    {
        if (string.IsNullOrEmpty(url))
            return;
        m_callBack = callBack;
        m_coroutine = CoroutineComponent.Instance.StartCoroutine(DoPostRequest(url, data, time));
    }

    public void Stop()
    {
        if (m_coroutine != null)
        {
            CoroutineComponent.Instance.StopCoroutine(m_coroutine);
        }
    }

    /// <summary>
    /// 等待消息返回
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    private IEnumerator DoGetRequest(string url,int time=3)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        //request.method = UnityWebRequest.kHttpVerbGET;
        P.D("WebRequestHelper SendGetRequest: " + url);
        request.timeout = time;
        yield return request.SendWebRequest();
        OnMsg(request);
    }

    private IEnumerator DoPostRequest(string url, string data, int time = 3)
    {
        //WWWForm form = new WWWForm();
        //byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(data);
        P.D("WebRequestHelper SendPostRequest: " + url + "\n" + data);
        UnityWebRequest request = UnityWebRequest.Put(url, data);
        request.method = UnityWebRequest.kHttpVerbPOST;
        request.SetRequestHeader("context-type", "application/json");
        request.timeout = time;
        yield return request.SendWebRequest();
        OnMsg(request);
    }

    private void OnMsg(UnityWebRequest request)
    {
        if (null == request.error)
        {
            string retData = request.downloadHandler.text;
            RemoveBomHead(ref retData);
            P.D($"WebRequestHelper OnMsg:{request.url}:\n{retData}");

            DoCallback(true, retData);
            request.Dispose();
        }
        else
        {
            P.D($"WebRequestHelper OnMsg error:{request.url}:\n{request.error}");
            DoCallback(false, request.error);
            request.Dispose();
        }
    }

    private void DoCallback(bool succ, string str)
    {
        if (m_callBack != null)
        {
            m_callBack(succ, str);
        }
    }

    private void RemoveBomHead(ref string str)
    {
        byte[] data = Encoding.UTF8.GetBytes(str);
        if (data.Length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
        {
            str = Encoding.UTF8.GetString(data, 3, data.Length - 3);
        }
        else
        {
            str = Encoding.UTF8.GetString(data);
        }
    }
}
